{% extends 'base.html'%}

{% block script %}
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {

            $('.vDateField').datepicker();


            $('a[name=add]').live('click', function(event) {
                event.preventDefault();
                var self = $(this);
                var tf = $('[name$=TOTAL_FORMS]', self.parents('form'));

                var form_name = tf.attr('name').split('-')[0];

                var tr = self.parents('tr:first').clone();
                var inputs = $('input,select,checkbox', tr)

                inputs.val('').removeAttr('checked').removeAttr('selected');
                inputs.each(function() {
                    var name = form_name+'-'+tf.val()+'-'+$(this).parent().attr('name');
                    var id = 'id_'+name;
                    $(this).attr('id', id).attr('name', name);
                });

                tf.val(parseInt(tf.val())+1);
                self.parents('tbody').append(tr);
            });

            $('a[name=rem]').live('click', function(event) {
                event.preventDefault();
                $(this).parents('tr').hide();
                $(this).parents('tr').find('[name$=DELETE]').attr('checked', true);
            });
            
        });
    </script>
{% endblock %}

{% block conteudo %}
<h1>Abrir Pregão</h1>

<form id="pregao" action="." method="post">{% csrf_token %}
    {{ ap }}

    <h2>Itens</h2>
    <table id="itens">
    <tbody>
    {% for fip in ip.forms %}
        <tr>
            {% for field in fip %}
                <td {% if field.is_hidden or field.name == 'DELETE' %}style="display:none;"{%endif%} name="{{field.name}}">
                    <span>{{field.label}}</span>
                    <br />
                    {{field}}
                </td>
                {% if forloop.last %}
                    <td name="actions">
                        <a href="#" name="add"><img src="{{ MEDIA_URL }}img/add_button.png" /></a>
                        <br />
                        <a href="#" name="rem"><img src="{{ MEDIA_URL }}img/cross_button.png" /></a>
                    </td>
                {% endif %}
            {% endfor %}
        </tr>
    {% endfor %}
    </tbody>
    </table>

    {{ ip.management_form }}

    <button class="positive" type="submit" value="Salvar"/>Salvar</button>
</form>

{% endblock %}
